<?php

namespace addon\hotel\api\controller;

use app\api\controller\BaseApi;
use addon\hotel\model\HotelCollect as HotelCollectModel;

class Hotelcollect extends BaseApi
{

    /**
     * 添加信息
     */
    public function add()
    {
        $token = $this->checkToken();
        if ($token['code'] < 0) return $this->response($token);
        $hotel_id = isset($this->params[ 'hotel_id' ]) ? $this->params[ 'hotel_id' ] : 0;
        if (empty($hotel_id)) {
            return $this->response($this->error('', '缺少参数酒店id'));
        }

        $goods_collect_model = new HotelCollectModel();
        $data = [
            'member_id' => $this->member_id,
            'hotel_id' => $hotel_id
        ];
        $res = $goods_collect_model->addCollect($data);

        return $this->response($res);
    }

    /**
     * 删除信息
     */
    public function delete()
    {
        $token = $this->checkToken();
        if ($token['code'] < 0) return $this->response($token);

        $hotel_id = isset($this->params[ 'hotel_id' ]) ? $this->params[ 'hotel_id' ] : 0;
        if (empty($hotel_id)) {
            return $this->response($this->error('', '缺少参数酒店id'));
        }
        $goods_collect_model = new HotelCollectModel();
        $res = $goods_collect_model->bathDeleteCollect($token['data']['member_id'], $hotel_id);
        return $this->response($res);

    }

    /**
     * 分页列表信息
     */
    public function page()
    {
        $token = $this->checkToken();
        if ($token['code'] < 0) return $this->response($token);

        $page = isset($this->params['page']) ? $this->params['page'] : 1;
        $page_size = isset($this->params['page_size']) ? $this->params['page_size'] : PAGE_LIST_ROWS;
        $lat = isset($this->params[ 'lat' ]) ? $this->params[ 'lat' ] : ""; // 纬度
        $lng = isset($this->params[ 'lng' ]) ? $this->params[ 'lng' ] : ""; // 经度

        $goods_collect_model = new HotelCollectModel();
        $condition = [
            ['hc.member_id', '=', $token['data']['member_id']],
        ];

        $list = $goods_collect_model->getCollectPageList($condition, $page, $page_size);
        if (!empty($list[ 'data' ][ 'list' ])) {
            foreach ($list[ 'data' ][ 'list' ] as $k => $item) {

                if ($item[ 'longitude' ] && $item[ 'latitude' ] && $lng && $lat) {
                    $list[ 'data' ][ 'list' ][ $k ][ 'distance' ] = round(getDistance((float) $item[ 'longitude' ], (float) $item[ 'latitude' ], (float) $lng, (float) $lat));
                } else {
                    $list[ 'data' ][ 'list' ][ $k ][ 'distance' ] = 0;
                }
            }
        }
        return $this->response($list);

    }

    /**
     * 是否收藏
     * @return string
     */
    public function iscollect()
    {
        $token = $this->checkToken();
        if ($token['code'] < 0) return $this->response($token);

        $hotel_id = isset($this->params[ 'hotel_id' ]) ? $this->params[ 'hotel_id' ] : 0;
        if (empty($hotel_id)) {
            return $this->response($this->error('', '缺少参数酒店id'));
        }

        $goods_collect_model = new HotelCollectModel();
        $res = $goods_collect_model->getIsCollect($hotel_id, $token['data']['member_id']);
        return $this->response($res);
    }

}